package com.boot.system.service.Impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.framework.constants.PageConstants;
import com.boot.system.dto.SysUserDTO;
import com.boot.system.entity.SysUserEntity;
import com.boot.system.mapper.SysUserMapper;
import com.boot.system.wrapper.SysUserWrapper;
import com.boot.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author yangfan
 * @date 2023-12-25 20:53:34
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements ISysUserService {
    private QueryWrapper<SysUserEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<SysUserEntity> wrapper = new QueryWrapper<>();
        String username = (String) params.get("username");
        wrapper.like(StrUtil.isNotEmpty(username), "username", username);
        return wrapper;
    }

    private Page<SysUserEntity> getPage(Map<String, Object> params) {
        long pageNum = Long.parseLong(String.valueOf(params.get(PageConstants.PAGE)));
        long pageSize = Long.parseLong(String.valueOf(params.get(PageConstants.LIMIT)));
        return new Page<>(pageNum, pageSize);
    }

    @Override
    public IPage<SysUserDTO> selectPage(Map<String, Object> params) {
        return SysUserWrapper.build().pageDTO(baseMapper.selectPage(getPage(params), getQueryWrapper(params)));
    }

    @Override
    public List<SysUserDTO> selectList(Map<String, Object> params) {
        return SysUserWrapper.build().listDTO(baseMapper.selectList(getQueryWrapper(params)));
    }

    @Override
    public SysUserDTO selectByUsername(String username) {

        return SysUserWrapper.build().entityDTO(baseMapper.selectOne(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getUsername, username)));
    }
}